﻿package ru.spesit.ads.mailru 
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	import flash.utils.*;
	
	/**
	 * Class RichMediaBanner
	 * Контейнер для RichMedia баннера с возможновстью его закрытия
	 * обчное местоположение по центру приложения
	 * размеры загружаемого баненра 420х180, размер контейнера 430х200;
	 * 
	 * @author Yuriy (Mur4ik) Kulkoff
	 * @version 0.1
	 * @date 14.06.2010
	 */
	public class RichMediaBanner extends Sprite
	{
		private var _userData:XML;
		private var _bannerLink:String;
		private var _loader:Loader;
		
		private var _banner:SpesItAds_RichMediaBanner;
		private var _bg:Sprite;
		
		public function RichMediaBanner()
		{
			_loader = new Loader();
			_banner = new SpesItAds_RichMediaBanner();
			
			_bg = new Sprite();
			_bg.mouseChildren = false;
			_bg.mouseEnabled = false;
			addChild(_bg);
			
			if (stage) bannerInit(null);
			else addEventListener(Event.ADDED_TO_STAGE, bannerInit);
		}
		
		private function bannerInit(event:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, bannerInit);
			
			addChild(_banner);
			
			stage.addEventListener(Event.RESIZE, stage_resizeHandler);
			stage_resizeHandler();
			
			var vars:URLVariables = new URLVariables();
			var ur:URLRequest = new URLRequest(AdsConfig.BANNER_URL);
			var ul:URLLoader = new URLLoader();
			
			_banner.mcCloseButton.mouseChildren = false;
			_banner.mcCloseButton.buttonMode = true;
			_banner.mcCloseButton.addEventListener(MouseEvent.CLICK, close_clickHandler);
			
			_banner.mcRichMedialBannerContainer.addChild(_loader);
			_banner.mcRichMedialBannerContainer.mouseChildren = false;
			_banner.mcRichMedialBannerContainer.mouseEnabled = false;
			
			_banner.btLink.addEventListener(MouseEvent.CLICK, onClick);
			
			_userData = <response/>;
			
			_userData.@set = "3";
			_userData.@session = AdsConfig.session;
			_userData.@pid = AdsConfig.PID;
			
			ur.contentType = "text/xml";
			ur.data = '<?xml version="1.0" encoding="utf-8"?>' + _userData.toXMLString();
			ur.method =  URLRequestMethod.POST;
			
			ul.addEventListener(Event.COMPLETE, data_loadCompleteHandler);
			ul.addEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			ul.load(ur);
			
			//trace(userData.toString());
		}
		
		public function setSize(w:Number, h:Number):void
		{
			_bg.graphics.clear();
			_bg.graphics.beginFill(0x000000, .5);
			_bg.graphics.drawRect(0, 0, w, h);
			_bg.graphics.endFill();
			
			_banner.x = int((w - /*_banner.width*/420) * .5);
			_banner.y = int((h - /*_banner.height*/180 ) * .5) - 50;
		}
		
		private function close_clickHandler(event:MouseEvent):void 
		{
			_loader.contentLoaderInfo.removeEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
			
			_banner.mcCloseButton.removeEventListener(MouseEvent.CLICK, close_clickHandler);
			
			stage.removeEventListener(Event.RESIZE, stage_resizeHandler);
			
			parent.removeChild(this);
		}
		
		private function data_loadCompleteHandler(event:Event):void
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, data_loadCompleteHandler);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			
			var xml:XML = new XML(event.target.data);
			var context:LoaderContext = new LoaderContext();
			
			_loader.load( new URLRequest(xml.@url.toString()), context );
			_loader.contentLoaderInfo.addEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
			
			_bannerLink = xml.@link.toString();
		}
		
		private function banner_loadErrorHandler(event:IOErrorEvent):void 
		{
			event.currentTarget.removeEventListener(Event.COMPLETE, data_loadCompleteHandler);
			event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			
			_loader.contentLoaderInfo.removeEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
			
			close_clickHandler(null);
		}
		
		private function banner_loadProgressHandler(event:ProgressEvent):void
		{
			var percent:uint = Math.round( 100 * (event.bytesLoaded / event.bytesTotal) );
			_banner.mcRichMedialBannerContainer.txtLoader.text = "Загружено " + String(percent) + "%";
		}
		
		private function banner_loadCompleteHandler(event:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.INIT, banner_loadCompleteHandler);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, banner_loadErrorHandler);
			_loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, banner_loadProgressHandler);
		}
		
		private function onClick(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			
			navigateToURL( new URLRequest(_bannerLink), "_blank" );
		}
		
		private function stage_resizeHandler(event:Event = null):void
		{
			setSize(stage.stageWidth, stage.stageHeight);
		}
	}
}